package com.synchrona.common;

import edu.emory.mathcs.backport.java.util.LinkedList;

public class Queue {

	private static final Queue queueInstance = new Queue();
	private static LinkedList queue = new LinkedList() ;
	private static boolean addedState = false;
	
	private Queue(){
		
	}
	
	public static synchronized Queue getQueue(){
		return queueInstance;
	}
	
	//enqueues the objects to the singleton queue
	public static synchronized  void enqueue(Object object){
		queue.addLast(object);
		setAddedState(true);
	}
	
	//Dequeues the queue
	public static synchronized Object dequeue(){
		Object object = null;
		if(!queue.isEmpty()){
			setAddedState(true);
			object = (Object)queue.removeFirst();
			if(queue.size()==0){
				setAddedState(false);
			}
		}
		else{
			setAddedState(false);
		}
		return object;
	}
	
	//Returns the size of the queue
	public static int Size(){
		return queue.size();
	}
	
	//Sets the state of the queue - enqueues/empty
	public static void setAddedState(boolean addedState) {
		Queue.addedState = addedState;
	}
	
	//Returns the state of the queue
	public static boolean isAddedState() {
		return addedState;
	}
}
